Skip to content

Leave alone a long literal under a cast, .new or method reference - #256

Merged
abashev merged 1 commit into
21.X.Xfrom
abashev/a-long-string-literal-under-a-cast-a-qualified
Nov 23, 2026
Merged

abashev merged 1 commit into
21.X.Xfrom
abashev/a-long-string-literal-under-a-cast-a-qualified

Conversation

@abashev

@abashev abashev commented Nov 22, 2026

Copy link
Copy Markdown
Collaborator

Closes #252. Based on 21.X.X, after #254; no other open pull request touches these files.

A long string literal under a cast, as the enclosing expression of a qualified .new, or as the qualifier of a method reference made the formatter fail the whole file. StringWrapper split it into "a" + "b", which leaves the cast only the first piece and the .new or the :: only the last. The AST check at the end of wrapOnce caught that and threw "Something has gone terribly wrong".

LongStringsAndTextBlockScanner.visitLiteral already skipped a literal that is the expression of a member select, "…".length(), for the same reason. That check is now bindsTighterThanPlus, which also skips the three contexts above. As the issue proposes, the literal is left alone rather than wrapped in parentheses that nobody wrote.

What changes for users

The file from the issue now formats:

class T {
    Object o = (Object)
            "one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt or to say anything";
    Object p =
            "one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt or to say anything"
            .new Inner();
    java.util.function.IntSupplier s =
            "one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt or to say anything"
                    ::length;
    String fine =
            "one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt"
                    + " or to say anything";
}

The first three literals come out as --skip-reflowing-long-strings lays them out, on a line of their own past the column limit. A literal in parentheses, (Object) ("…"), is still reflowed inside them.

Nothing else changes: before, such a literal either had nothing to reflow or made wrapOnce throw.

"…"[0] and unary operators such as -"…" bind tighter than + too, but they do not compile on a String, so they are left out.

Tests

Without the fix, all four fail with "Something has gone terribly wrong".

With the change, JDK 21: ./gradlew check passes.

Reflowing a long string literal failed the whole file when the
literal sat under something that binds tighter than `+`: a cast, the
enclosing expression of a qualified `.new`, or a method reference.
Split into "a" + "b", the literal left the cast only its first piece,
as in ((Object) "a") + "b", and the `.new` or the `::` only its last.
The AST check at the end of wrapOnce caught this and threw, so the
formatter printed "Something has gone terribly wrong" and formatted
nothing in the file.

LongStringsAndTextBlockScanner already left alone the expression of a
member select ("...".length()) for this reason. It now skips the
three other contexts too, and such a literal stays on its line as the
formatter lays it out. A literal in parentheses, as in
(Object) ("..."), is still reflowed inside them.
@abashev
abashev merged commit 5bac7a2 into 21.X.X Nov 23, 2026
16 checks passed
@abashev
abashev deleted the abashev/a-long-string-literal-under-a-cast-a-qualified branch November 23, 2026 09:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A long string literal under a cast, a qualified .new or a method reference fails the whole file

1 participant